package com.xcoder.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://leetcode.cn/problems/implement-stack-using-queues/
 */
public class _225_用队列实现栈 {

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop());
        System.out.println(stack.top());
        System.out.println(stack.empty());
    }

    private static class MyStack {
        private Queue<Integer> inQueue;
        private Queue<Integer> outQueue;

        /**
         * Initialize your data structure here.
         */
        public MyStack() {
            inQueue = new LinkedList<>();
            outQueue = new LinkedList<>();
        }

        /**
         * Push element x onto stack.
         */
        public void push(int x) {
            inQueue.add(x);
        }

        /**
         * Removes the element on top of the stack and returns that element.
         */
        public int pop() {
            checkOutQueue();
            return outQueue.poll();
        }

        /**
         * Get the top element.
         */
        public int top() {
            checkOutQueue();
            return outQueue.peek();
        }

        private void checkOutQueue() {
            if (outQueue.isEmpty()) {
                while (!inQueue.isEmpty()) {
                    outQueue.offer(inQueue.poll());
                }
            }
        }

        /**
         * Returns whether the stack is empty.
         */
        public boolean empty() {
            return inQueue.isEmpty() && outQueue.isEmpty();
        }
    }
}


